public class AddBinary {

    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer("123");
        System.out.println(stringBuffer.substring(3,3));
    }

    public String addBinary(String a, String b) {
        if (a.length() < b.length()) return addBinary(b, a);
        StringBuffer ans = new StringBuffer();
        StringBuffer a1 = new StringBuffer(a).reverse();
        StringBuffer b1 = new StringBuffer(b).reverse();
        boolean flag = false;
        for (int i = 0; i < b1.length(); i++) {
            char c1 = a1.charAt(i);
            char c2 = b1.charAt(i);
            if (c1 == '1' && c2 == '1') {
                if (flag) {
                    ans.append("1");
                } else ans.append("0");
                flag = true;
            } else {
                if (c1 == '0' && c2 == '0') {
                    if (flag) {
                        ans.append("1");
                    } else {
                        ans.append("0");
                    }
                    flag = false;
                } else {
                    if (flag) {
                        ans.append("0");
                        flag = true;
                    } else {
                        ans.append("1");
                        flag = false;
                    }
                }
            }
        }
        if (!flag) {
            ans.append(a1.substring(b1.length(), a1.length()));
            return ans.reverse().toString();
        }
        for (int i = b1.length(); i < a1.length(); i++) {
            if (a1.charAt(i) == '0') {
                ans.append("1");
                ans.append(a1.substring(i + 1, a1.length()));
                return ans.reverse().toString();
            }
            ans.append("0");
        }
        ans.append("1");
        return ans.reverse().toString();
    }
}
